﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using HNQXYJ.Model;

namespace HNQXYJ.DAL
{
    public  class HistoryInforDAL
    {
        /// <summary>
        /// 返回历史信息表数据
        /// </summary>
        /// <param name="procName"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public static DataTable GetHistroyTable(string procName, params SqlParameter[] paras)
        {
            DataTable dt = SqlHelper.GetDataTable(procName, paras);
            return dt;
 
        }

        /// <summary>
        /// 向历史信息表中插入数据
        /// </summary>
        /// <param name="procName"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public  int ExecNoQuery(string procName, params SqlParameter[] paras)
        {
            int res = SqlHelper.ExecNonQuery(procName, paras);
            return res;
        }

        private HistoryInfor ToModel(DataRow dr)
        {
            HistoryInfor hi = new HistoryInfor();
            hi.ID = Convert.ToInt32(dr["Id"]);
            hi.Phone = dr["Phone"].ToString();
            hi.UserName = dr["UserName"].ToString();
            hi.IsSuccess = dr["IsSuccess"].ToString();
            hi.Information = dr["Information"].ToString();
            hi.DateAdd =Convert.ToDateTime( dr["DateAdd"]);
            hi.RankLevel = dr["RankLevel"].ToString();
            return hi;
        }

        /// <summary>
        /// 向历史信息表中插入数据
        /// </summary>
        /// <param name="hi"></param>
        /// <returns></returns>
        public static int InsertDataMessage(string phone,string username,string information,string isSuccess,string ranklevel)
        {
            string procName = "HistoryInforInsertMessages";
            SqlParameter[] paras = { 
                                   new SqlParameter("@Phone",SqlDbType.VarChar,100),
                                   new SqlParameter("@UserName",SqlDbType.NVarChar,50),
                                   new SqlParameter("@Information",SqlDbType.NVarChar,500),
                                   new SqlParameter("@IsSuccess",SqlDbType.NVarChar,50),
                                   new SqlParameter("@RankLevel",SqlDbType.VarChar,100)
                                   };
            paras[0].Value = phone;
            paras[1].Value =username;
            paras[2].Value =information;
            paras[3].Value = isSuccess;
            paras[4].Value =ranklevel;

            int res = SqlHelper.ExecNonQuery(procName, paras);
            return res;                     
        }

        /// <summary>
        /// 历史信息分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rankLevel"></param>
        /// <param name="rowCount"></param>
        /// <param name="pageCount"></param>
        /// <returns></returns>
        public List<HistoryInfor> GetDataPagedRankLevel(int pageIndex, int pageSize, string rankLevel, out int rowCount, out int pageCount)
        {
            rowCount = 0;
            pageCount = 0;
            string procName = "HistoryInforGetAllListPaged";
            SqlParameter[] paras = { 
                                    new SqlParameter("@PageIndex",SqlDbType.Int),
                                    new SqlParameter("@PageSize",SqlDbType.Int),
                                    new SqlParameter("@RankLevel",SqlDbType.VarChar,100),
                                    new SqlParameter("@RowCount",SqlDbType.Int),
                                    new SqlParameter("@PageCount",SqlDbType.Int)
                                   };
            paras[0].Value = pageIndex;
            paras[1].Value = pageSize;
            paras[2].Value = rankLevel;
            paras[3].Value = rowCount;
            paras[3].Direction = ParameterDirection.Output;
            paras[4].Value = pageCount;
            paras[4].Direction = ParameterDirection.Output;

            DataTable dt = SqlHelper.GetDataTable(procName, paras);
            if (dt.Rows.Count > 0)
            {
                List<HistoryInfor> List = new List<HistoryInfor>();
                foreach (DataRow dr in dt.Rows)
                {
                    List.Add(ToModel(dr));
                }
                rowCount = Convert.ToInt32(paras[3].Value);
                pageCount = Convert.ToInt32(paras[4].Value);
                return List;
            }
            else
            {
                return null;
            }
        }
    }
}